//
//  YFBAlertTipsView.swift
//  YFBUIKit
//
//  Created by admin on 2025/9/5.
//

import UIKit
import SnapKit


public typealias YFBAlertDoneBlock = () -> ()
public typealias YFBAlertCancelBlock = () -> ()

open class KBAlertView: UIView {
    
    fileprivate var doneBlock: YFBAlertDoneBlock?
    fileprivate var cancelBlock: YFBAlertCancelBlock?
    /// 屏幕的宽
    let ALERTSCREENW = UIScreen.main.bounds.size.width
    /// 屏幕的高
    let ALERTSCREENH = UIScreen.main.bounds.size.height
    
    open var sureIsRemove: Bool = true //点击确定按钮隐藏还是移除 默认移除
    open var cancelIsRemove: Bool = true //点击取消按钮隐藏还是移除 默认移除
    
    
    //MARK: - 系统方法
    // 系统初始化
    public convenience init(title: String? = nil,_ message: String?, lineBreakMode: NSLineBreakMode = .byCharWrapping, cancelButtonTitle: String? = nil, otherButtonTitle: String, doneBlock: @escaping YFBAlertDoneBlock, cancelBlock: @escaping YFBAlertCancelBlock) {
        self.init()
        self.doneBlock = doneBlock
        self.cancelBlock = cancelBlock
        
        self.titleLabel.text = title
        self.contentLabel.text = message
        self.contentLabel.lineBreakMode = lineBreakMode
        self.cancelButton.setTitle(cancelButtonTitle, for: .normal)
        self.sureButton.setTitle(otherButtonTitle, for: .normal)
        
        self.alert_setView()
    }
    
    func alert_setView() {
        
        self.frame = CGRect(x: 0, y: 0, width: ALERTSCREENW, height: ALERTSCREENH)
        let bacView = UIView()
        bacView.backgroundColor = UIColor.white
        bacView.clipsToBounds = true
        bacView.layer.cornerRadius = 10
        self.addSubview(bacView)
        bacView.addSubview(titleLabel)
        bacView.addSubview(contentLabel)
        bacView.addSubview(cancelButton)
        bacView.addSubview(sureButton)
        bacView.addSubview(verLineView)
        bacView.addSubview(horLineView)
        
        var cancelBtnWidth = ALERTSCREENW / 2.0 - 35
        if ((self.cancelButton.titleLabel?.text == nil) || (self.cancelButton.titleLabel?.text?.count == 0)) {
            cancelBtnWidth = 0
            verLineView.isHidden = true
        }
        
        let attributes = [NSAttributedString.Key.font: UIFont(name: "PingFangSC-Regular", size: 14)]
        let message_NS = NSString(string: self.contentLabel.text ?? "")
        let size = message_NS.boundingRect(with: CGSize(width: ALERTSCREENW - 130, height: 0.00),
                                           options: .usesLineFragmentOrigin,
                                           attributes: attributes as [NSAttributedString.Key : Any],
                                           context: nil).size
        
        
        //添加约束
        bacView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(35)
            make.right.equalToSuperview().offset(-35)
            make.centerY.equalToSuperview()
        }
        
        var titleHeight = 22
        var titleTop = 20
        if((self.titleLabel.text == nil) || (self.titleLabel.text?.count == 0)){
            titleHeight = 0
            titleTop = 0
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(titleTop)
            make.height.equalTo(titleHeight)
            make.centerX.equalToSuperview()
        }
        
        cancelButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(0)
            make.width.equalTo(cancelBtnWidth)
            make.height.equalTo(50)
            make.bottom.equalToSuperview().offset(0)
        }
        verLineView.snp.makeConstraints { make in
            make.left.equalTo(cancelButton.snp.right).offset(0)
            make.bottom.equalToSuperview().offset(0)
            make.height.equalTo(50)
            make.width.equalTo(0.5)
        }
        
        sureButton.snp.makeConstraints { make in
            make.left.equalTo(verLineView.snp.right).offset(0)
            make.height.equalTo(50)
            make.right.equalToSuperview().offset(0)
            make.bottom.equalToSuperview().offset(0)
        }
        
        horLineView.snp.makeConstraints { make in
            make.bottom.equalTo(sureButton.snp.top).offset(0)
            make.height.equalTo(0.5)
            make.left.equalToSuperview().offset(0)
            make.right.equalToSuperview().offset(0)
        }
        
        var contentTop = 30
        var contentBottom = 30
        if ((size.height < 30) || (titleHeight == 0)){ //文本内容如果是一行的时候
            contentTop = 40
            contentBottom = 40
        }
        
        
        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(contentTop)
            make.bottom.equalTo(horLineView.snp.top).offset(-contentBottom)
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
        }
        
    }
    
    /// 创建容器
    lazy var bacView: UIView = {
        let bacView = UIView()
        bacView.backgroundColor = UIColor.white
        bacView.clipsToBounds = true
        bacView.layer.cornerRadius = 10
        return bacView
    }()
    
    /// 创建标题文本
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.textColor = UIColor(valueRGB: 0x333333, alpha: 1.0)
        titleLabel.font = UIFont(name: "PingFangSC-Medium", size: 16)
        titleLabel.textAlignment = .center
        return titleLabel
    }()
    
    /// 创建内容文本
    lazy var contentLabel: UILabel = {
        let contentLabel = UILabel()
        contentLabel.textColor = UIColor(valueRGB: 0x666666, alpha: 1.0)
        contentLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
        contentLabel.textAlignment = .center
        contentLabel.numberOfLines = 0
        contentLabel.adjustsFontSizeToFitWidth = true
        return contentLabel
    }()
    
    /// 创建取消按钮
    lazy var cancelButton: UIButton = {
        let cancelButton = UIButton()
        cancelButton.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 16)
        cancelButton.addTarget(self, action: #selector(cancelButtonClicked), for: .touchUpInside)
        cancelButton.setTitleColor(UIColor(valueRGB: 0x999999, alpha: 1.0), for: .normal)
        return cancelButton
    }()
    
    /// 创建确定按钮
    lazy var sureButton: UIButton = {
        let sureButton = UIButton()
        sureButton.setTitle("确定", for: .normal)
        sureButton.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 16)
        sureButton.addTarget(self, action: #selector(sureButtonClicked), for: .touchUpInside)
        sureButton.setTitleColor(doneButtonTitleColor, for: .normal)
        return sureButton
    }()
    
    lazy var verLineView: UIView = {
        let verLineView = UIView()
        verLineView.backgroundColor = UIColor(valueRGB: 0xeeeeee, alpha: 1.0)
        return verLineView
    }()
    
    lazy var horLineView: UIView = {
        let horLineView = UIView()
        horLineView.backgroundColor = UIColor(valueRGB: 0xeeeeee, alpha: 1.0)
        return horLineView
    }()
    
    open var doneButtonTitleColor: UIColor = UIColor.gray {
        didSet {
            sureButton.setTitleColor(doneButtonTitleColor, for: .normal)
        }
    }

    //MARK: 取消按钮点击事件
    @objc func cancelButtonClicked() {
        self.hideView(isRemove: self.cancelIsRemove)
        cancelBlock?()
    }
    
    @objc func sureButtonClicked() {
        self.hideView(isRemove: self.sureIsRemove)
        doneBlock?()
    }
    
    open func showView() {
        UIApplication.shared.keyWindow?.addSubview(self)
        backgroundColor = UIColor.clear
        UIView.animate(withDuration: 0.25) {
            self.backgroundColor = UIColor(valueRGB: 0x000000, alpha: 0.3)
        }
    }
    
    open func hideView(isRemove: Bool) {
        UIView.animate(withDuration: 0.25, animations: {
//            self.backgroundColor = UIColor.clear
        }) { (finish) in
            if(isRemove){
                self.removeFromSuperview()
            }else{
                
            }
        }
    }
    
    public func setContentTextAligment(alignment: NSTextAlignment){
        self.contentLabel.textAlignment = alignment
    }
    
}
